const fp = require('lodash/fp');
/* 基于以下代码完成下面的四个练习 */
const cars = [
  {
    name: 'Ferrari FF',
    horseepower: 110,
    dollar_value: 100000,
    in_skock: true,
  },
  {
    name: 'Jaguar XKR-S',
    horseepower: 220,
    dollar_value: 200000,
    in_skock: true,
  },
  { name: 'Audi R8', horseepower: 330, dollar_value: 300000, in_skock: false },
];

// 练习1：使用函数组合fp.flowRight()重新实现下面的函数
// let isLastInStork = cars => {
//   let last_car = fp.last(cars);
//   return fp.prop('in_skock', last_car);
// };

const getLastInStorkFun = fp.flowRight(fp.prop('in_skock'), fp.last);
console.log(getLastInStorkFun(cars));

// 练习2 获取第一个car的name

const getFirstNameFun = fp.flowRight(fp.prop('name'), fp.first);
console.log(getFirstNameFun(cars));

// 练习3
let _average = xs => {
  return fp.reduce(fp.add, 0, xs) / xs.length;
};
// let averageDollarValue = cars => {
//   let dollar_values = fp.map(car => car.dollar_value, cars);
//   return _average(dollar_values);
// };
// console.log(averageDollarValue(cars));

const getAverageDollarValueFun = fp.flowRight(
  _average,
  fp.map(fp.prop('dollar_value'))
);
console.log(getAverageDollarValueFun(cars));

// 练习4
const _underscore = fp.replace(/\W+/g, '_');
const sanitizeNames = fp.flowRight(
  fp.map(fp.flowRight(_underscore, fp.prop('name')))
);
console.log(sanitizeNames(cars));
